﻿using System;
using System.Collections.Generic;
//using System.Linq;
using BaseModel;
using System.Threading;
using WellLogShower;

namespace LoggingSender
{
    public class RemoteHandler: MarshalByRefObject
    {
        public static Form1 form1;
        private delegate void Add();//定义一个线程委托
        private Thread t = null; //定义线程变量
        private WellLoggingPoint p;
        private WellLoggingInfo[] wellLoggingInfoes;
        private Equipment equipment;
        private WellInfo well;

        public RemoteHandler()
        {
           
        }

        public void initLoggingInfo(Equipment eqm, WellInfo wi)
        {
            t = new Thread(InitThread);
            t.IsBackground = true;
            t.Start();
            equipment = eqm;
            well = wi;
        }

        public void StartSendLoggingData(WellLoggingInfo[] wli)
        {
            t = new Thread(StartThread);
            t.IsBackground = true;
            t.Start();
            wellLoggingInfoes = wli;
        }

        public bool SendData(WellLoggingPoint gp)
        {
            t = new Thread(SendDataThread);
            t.IsBackground = true;
            t.Start();
            p = gp;
            return true;
        }

        public void endLoggingData()
        {
            t = new Thread(EndThread);
            t.IsBackground = true;
            t.Start();
        }

        private void InitThread()
        {
            Add fc = new Add(InitThreadMethod); //将代理绑定到方法
            form1.BeginInvoke(fc);//调用代理
        }

        private void InitThreadMethod()
        {
            form1.Data.WellName = well.Name;
            form1.Data.StartMD = well.StartMD;
            form1.listbox.Items.Add("-------   数据采集初始化  --------");
            t.Abort();
            t = null;
        }

        private void SendDataThread()
        {
            Add fc = new Add(SendDataThreadMethod);            //将代理绑定到方法
            form1.BeginInvoke(fc);                  //调用代理
        }

        private void SendDataThreadMethod()
        {
            form1.listbox.Items.Add("-------");
            form1.listbox.Items.Add("测试深度：" + p.TestMD);
            form1.Data.LogData[0].Data.Add(p.TestMD);
            for (int i = 0; i < p.Value.Length; i++)
            {
                form1.Data.LogData[i + 1].Data.Add(p.Value[i]);
                form1.listbox.Items.Add(p.Value[i]);
            }
            form1.Invalidate();
            //t.Abort();
            //t = null;
        }

        private void StartThread()
        {
            Add fc = new Add(StartThreadMethod); //将代理绑定到方法
            form1.BeginInvoke(fc);//调用代理
        }

        private void StartThreadMethod()
        {
            form1.Data.LogData = new LogData[wellLoggingInfoes.Length + 1];
            form1.Data.LogData[0] = new LogData();
            form1.Data.LogData[0].Name = "Depth";
            form1.Data.LogData[0].Unit = "米";

            for (int i = 1; i <= wellLoggingInfoes.Length; i++ )
            {
                LogData l = new LogData();
                l.Name = wellLoggingInfoes[i - 1].Name;
                l.Unit = wellLoggingInfoes[i - 1].Unit;
                form1.Data.LogData[i] = l;
            }
            form1.listbox.Items.Add("-------    开始采集数据  --------");
            form1.listbox.Items.Add("采集曲线名称：");
            for (int i = 0; i < wellLoggingInfoes.Length; i++)
            {
                form1.listbox.Items.Add(wellLoggingInfoes[i].Name);
            }
            t.Abort();
            t = null;
        }

        private void EndThread()
        {
            Add fc = new Add(EndThreadMethod); //将代理绑定到方法
            form1.BeginInvoke(fc);//调用代理
        }

        private void EndThreadMethod()
        {
            form1.listbox.Items.Add("-------    结束采集数据  --------");
            t.Abort();
            t = null;
        }

        ~RemoteHandler()
        {
            if (t != null)
            {
                t.Abort();
                t = null;
            }
        }
    }
}
